//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.IO.Packaging;
using System.Xml;
using OpenLS.Chart.Serialization;
using OpenLS.Core.Serialization;
using OpenLS.Drawing;
using OpenLS.Drawing.Serialization;

namespace OpenLS.Chart
{
    public class Graphic : IOfficeXmlSerializable
    {
        private ChartArea _chart;

        public ChartArea ChartSpace
        {
            get
            {
                if (_chart == null)
                    _chart = new ChartArea();
                return _chart;
            }
            set { _chart = value; }
        }

        #region IOfficeXmlSerializable Members

        void IOfficeXmlSerializable.ReadXml(ReadContext context)
        {
            using (ReadContext c1 = context.Read("graphic"))
            {
                using (ReadContext c2 = c1.Read("graphicData"))
                {
                    using (ReadContext c = c2.Read("chart"))
                    {
                        PackageRelationship r =
                            c.Part.GetRelationship(c.GetString(OfficeConstants.RelationshipNamespace + "id"));
                        PackagePart chartPart =
                            c.Part.Package.GetPart(PackUriHelper.ResolvePartUri(r.SourceUri, r.TargetUri));
                        ChartSpace = new ChartArea(); //\\chartPart.Uri);
                        using (XmlTextReader chartReader = new XmlTextReader(chartPart.GetStream()))
                        {
                            ReadContext crc = new ReadContext(chartReader, chartPart, ChartConstants.ChartNamespaceUri);
                            crc.CopyDataFrom(context);
                            IOfficeXmlSerializable i = ChartSpace;
                            i.ReadXml(crc);
                        }
                    }
                    //\\using (ReadElement ce = new ReadElement("chart", ChartConstants.ChartNamespace, reader)) { }
                }
            }
        }

        void IOfficeXmlSerializable.WriteXml(WriteContext context)
        {
            using (WriteContext graphicE = context.Write(DrawingConstants.DrawingNamespace + "graphic"))
            {
                using (WriteContext graphicDataE = graphicE.Write("graphicData"))
                {
                    graphicDataE.SetString("uri", ChartConstants.ChartNamespaceUri);
                    using (WriteContext chartE = graphicDataE.Write(ChartConstants.ChartNamespace + "chart"))
                    {
                        Uri uri = context.GetAvailableUri("./charts/chart{0}.xml"); //\\ok root?
                        using (
                            RelatedWriteContext ctx =
                                chartE.CreateRelatedContext(ChartConstants.ChartNamespace, uri,
                                                            ChartConstants.ChartRelationshipType,
                                                            ChartConstants.ChartContentType))
                        {
                           // chartE.SetString("id", "r", OfficeConstants.RelationshipNamespaceUri, ctx.Relationship.Id);
                            chartE.SetString(OfficeConstants.RelationshipNamespace + "id" , ctx.Relationship.Id);
                            ctx.WriteElement(ChartSpace);
                        }
                    }
                }
            }
        }

        #endregion
    }
}